package com.qinggeng.framework.config.mq;


import com.aliyun.openservices.ons.api.*;
import com.qinggeng.framework.config.mq.enums.MQTypeEnum;
import com.qinggeng.framework.config.mq.impl.*;
import com.qinggeng.framework.config.mq.impl.AccountAddMoneyHandleImpl;
import com.qinggeng.framework.config.mq.impl.AccountSubMoneyHandleImpl;
import com.qinggeng.framework.config.mq.impl.RedEnvelopesHandleImpl;
import com.qinggeng.framework.config.mq.listener.MessageListenerImpl;
import com.qinggeng.project.api.websocket.service.BulletChatUserAddCartServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;

@Configuration
public class ConsumerConfig  {
    @Value("${rocketmq.onsAddr}")
    private String onsAddr;

    @Value("${rocketmq.accessKey}")
    private String accessKey;

    @Value("${rocketmq.secretKey}")
    private String secretKey;

    @Value("${rocketmq.topic}")
    private  String topic;

    @Value("${rocketmq.delayedTopic}")
    private  String delayedTopic;

    @Value("${rocketmq.consumer}")
    private  String consumerId;
    @Value("${rocketmq.delayedConsumer}")
    private  String delayedConsumerId;


    //账户直接加钱
    @Autowired
    private AccountAddMoneyHandleImpl accountAddMoneyHandle;

    //账户直接捡钱
    @Autowired
    private AccountSubMoneyHandleImpl accountSubMoneyHandle;

    //红包
    @Autowired
    private RedEnvelopesHandleImpl redEnvelopesHandle;
    //拉新
    @Autowired
    private PullNewMoneyHandleImpl pullNewMoneyHandle;

    @Autowired
    private OrdersDistAutoTakeHandleImpl ordersDistAutoTakeHandle;

    //订单完成时候消费
    @Autowired
    private MessageOrderPayImpl messageOrderPay;

    //订单退款
    @Autowired
    private MessageOrderRefundImpl messageOrderRefund;

    @Autowired
    private AssistFailHandleImpl assistFailHandle;

    @Autowired
    private BeatABeatFailHandleImpl aBeatFailHandle;

    //月抽奖
    @Autowired
    private MonthLotteryMoneyHandleImpl monthLotteryMoneyHandle;

    //月抽奖转盘
    @Autowired
    private MonthLotteryTurntableMoneyHandleImpl monthLotteryTurntableMoneyHandle;



    //账户提现
    @Autowired
    private AccountRecordWithdrawalHandleImpl accountRecordWithdrawalHandle;

    //单商品退款
    @Autowired
    private OrderGoodsSingleItemRefundHandleImpl orderGoodsSingleItemRefundHandle ;

    //首页弹幕
    @Autowired
    private BulletChatHomeLoginHandleImpl  bulletChatHandleImpl;

    //中奖
    @Autowired
    private BulletChatLuckyDrawHandImpl luckyDrawHandImpl;

    //添加购物车
    @Autowired
    private BulletChatUserAddCartHandleImpl  userAddCartService;

    @Autowired
    private BulletChatGoodDetailHandleImpl  goodDetailHandle;

    @Autowired
    private PresaleHandleImpl presaleHandle;

    @Autowired
    private OrdersMessageJPushHandleImpl ordersMessageJPushHandle;

    @Autowired
    private GoodsRefundMessageJPushHandleImpl goodsRefundMessageJPushHandle;

    @Autowired
    private OrdersPrintOutTimeHandleImpl ordersPrintOutTimeHandle;

    @Autowired
    private UserGoodsLimitRecordHandleImpl userGoodsLimitRecordHandle;

    @Autowired
    private RedEnvelopesMessageJPushHandleImpl redEnvelopesMessageJPushHandle;

    @Autowired
    private OrdersAutoAffirmHandleImpl ordersAutoAffirmHandle;

    @Autowired
    private OrdersAutoEvaluateHandleImpl ordersAutoEvaluateHandle;

    @Autowired
    private OrdersEndStatusHandleImpl ordersEndStatusHandle;

    @Autowired
    private OrdersPayOutTimeHandleImpl ordersPayOutTimeHandle;

    @Autowired
    private ShareWithdrawalHandleImpl shareWithdrawalHandlel;

    @Autowired
    private AutoShipHandleImpl autoShipHandle;

    @Autowired
    private GoodsKeepReduceHandleImpl goodsKeepReduceHandle;

    @Autowired
    private PointSaleRecordHandleImpl pointSaleRecordHandle;

    @Autowired
    private OrderCommissionMoneyHandleImpl orderCommissionMoneyHandle;
    @Autowired
    private ShareMoneyHandleImpl shareMoneyHandle;

    @Autowired
    private GoodsPercentageUpdateHandleImpl goodsPercentageUpdateHandle;


    @Autowired
    private VipHandleImpl vipHandle;

    @Bean(name = "messageListenerImpl")
    public MessageListener messageListener(){
        MessageListenerImpl messageListener=new MessageListenerImpl();
        // 給賬戶加錢
        messageListener.registerHandleHashMap(MQTypeEnum.ADD_ACCOUNT.getType(),accountAddMoneyHandle);

        // 給賬戶減錢
        messageListener.registerHandleHashMap(MQTypeEnum.SUB_ACCOUNT.getType(),accountSubMoneyHandle);

        //用户拉新
        messageListener.registerHandleHashMap(MQTypeEnum.PULL_NEW_ACCOUNT.getType(),pullNewMoneyHandle);

        //用户红包
        messageListener.registerHandleHashMap(MQTypeEnum.RED_ENVRLOPES.getType(),redEnvelopesHandle);

        //更新订单打印状态
        messageListener.registerHandleHashMap(MQTypeEnum.UPDATE_PRINT_STATUS.getType(),ordersPrintOutTimeHandle);

        //订单自动接单
        messageListener.registerHandleHashMap(MQTypeEnum.ORDERS_AUTO_TAKE.getType(),ordersDistAutoTakeHandle);

        //订单消息推送
        messageListener.registerHandleHashMap(MQTypeEnum.ORDERS_MESSAGE_JPUSH.getType(),ordersMessageJPushHandle);

        //商品退款消息推送
        messageListener.registerHandleHashMap(MQTypeEnum.GOODS_REFUND_MESSAGE_JPUSH.getType(),goodsRefundMessageJPushHandle);

        //用户下单 消费
        messageListener.registerHandleHashMap(MQTypeEnum.ORDER_PAY_ACCOUNT.getType(),messageOrderPay);

        //用户退款
        messageListener.registerHandleHashMap(MQTypeEnum.ORDER_REFUND_ACCOUNT.getType(),messageOrderRefund);

        //月抽奖
        messageListener.registerHandleHashMap(MQTypeEnum.MONTH_LOTTERY_ACCOUNT.getType(),monthLotteryMoneyHandle);

        //月抽奖转盘
        messageListener.registerHandleHashMap(MQTypeEnum.MONTH_TURNTABLE_LOTTERY_ACCOUNT.getType(),monthLotteryTurntableMoneyHandle);

        //提现
        messageListener.registerHandleHashMap(MQTypeEnum.WITHDRAWAL_ACCOUNT.getType(),accountRecordWithdrawalHandle);

        //单商品退款
        messageListener.registerHandleHashMap(MQTypeEnum.SINGLE_LTEM_REFUND_ACCOUNT.getType(),orderGoodsSingleItemRefundHandle);

        //首页
        messageListener.registerHandleHashMap(MQTypeEnum.INDEX_BULLET_CHAT.getType(),bulletChatHandleImpl);

        //抽奖推送
        messageListener.registerHandleHashMap(MQTypeEnum.LUCKY_DRAW.getType(),luckyDrawHandImpl);

        //添加进入购物车
        messageListener.registerHandleHashMap(MQTypeEnum.USER_ADD_CART.getType(),userAddCartService);

        //添加进入购物车
        messageListener.registerHandleHashMap(MQTypeEnum.GOODS_DETAI.getType(),goodDetailHandle);

        //助力失败
        messageListener.registerHandleHashMap(MQTypeEnum.ASSIST_FAIL_HANDLE.getType(),assistFailHandle);

        //捶一捶失败
        messageListener.registerHandleHashMap(MQTypeEnum.BEAT_FAIL_HANDLE.getType(),aBeatFailHandle);

        //预售商品修改状态
        messageListener.registerHandleHashMap(MQTypeEnum.PRESALE_HANDLE.getType(),presaleHandle);

        //限购商品购买记录
        messageListener.registerHandleHashMap(MQTypeEnum.USER_GOODS_LIMIT_RECORD.getType(),userGoodsLimitRecordHandle);

        //红包消息推送
        messageListener.registerHandleHashMap(MQTypeEnum.RED_ENVELOPES_MESSAGE_JPUSH.getType(),redEnvelopesMessageJPushHandle);

        //订单自动确认收货
        messageListener.registerHandleHashMap(MQTypeEnum.ORDERS_AUTO_AFFIRM.getType(),ordersAutoAffirmHandle);

        //订单自动评价
        messageListener.registerHandleHashMap(MQTypeEnum.ORDERS_AUTO_EVALUATE.getType(),ordersAutoEvaluateHandle);

        //订单完结状态处理
        messageListener.registerHandleHashMap(MQTypeEnum.ORDERS_END_STATUS_UPDATE.getType(),ordersEndStatusHandle);

        //订单支付超时处理
        messageListener.registerHandleHashMap(MQTypeEnum.PAY_OUT_TIME.getType(),ordersPayOutTimeHandle);

        //分享商品提现处理
        messageListener.registerHandleHashMap(MQTypeEnum.SHARE_WITHDRAWAL.getType(),shareWithdrawalHandlel);

        //自动发货
        messageListener.registerHandleHashMap(MQTypeEnum.AUTO_SHIP.getType(),autoShipHandle);

        //扣减库存
        messageListener.registerHandleHashMap(MQTypeEnum.GOOD_KEEP_REDUCE.getType(),goodsKeepReduceHandle);

        //商品销量统计
        messageListener.registerHandleHashMap(MQTypeEnum.POINT_SALE_RECORD.getType(),pointSaleRecordHandle);

        //发送订单提成
        messageListener.registerHandleHashMap(MQTypeEnum.ORDER_COMMISSION_ACCOUNT.getType(),orderCommissionMoneyHandle);

        //发送订单提成
        messageListener.registerHandleHashMap(MQTypeEnum.SHARE_ACCOUNT.getType(),shareMoneyHandle);

        //商品提成
        messageListener.registerHandleHashMap(MQTypeEnum.GOODS_PERCENTAGE_UPDATE.getType(),goodsPercentageUpdateHandle);
        //购买vpi
        messageListener.registerHandleHashMap(MQTypeEnum.VIP.getType(),vipHandle);

        return  messageListener;
    }


    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public Consumer consumer() {
        Consumer consumer = ONSFactory.createConsumer( getProperties());
        consumer.subscribe(this.topic,"*",messageListener());
        return consumer;
    }

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public Consumer delayedTopic() {
        Consumer consumer = ONSFactory.createConsumer( getDelayedProperties());
        consumer.subscribe(this.delayedTopic,"*",messageListener());
        return consumer;
    }


    private Properties getProperties(){
        Properties properties = new Properties();
        // AccessKey ID 阿里云身份验证，在阿里云服务器管理控制台创建。
        properties.put(PropertyKeyConst.AccessKey, this.accessKey);
        // AccessKey Secret 阿里云身份验证，在阿里云服务器管理控制台创建。
        properties.put(PropertyKeyConst.SecretKey, this.secretKey);
        properties.put(PropertyKeyConst .NAMESRV_ADDR, onsAddr);
        properties.put(PropertyKeyConst.GROUP_ID,consumerId);
        properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.CLUSTERING);
        return  properties;
    }

    private Properties getDelayedProperties(){
        Properties properties=getProperties();
        properties.put(PropertyKeyConst.GROUP_ID,delayedConsumerId);
        return  properties;
    }
}

